package com.sc.util;



import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Description:
 * @Author: Rui
 * @Date: 2024/10/7 19:59
 */

public class FileUpLoad {

    public static String upLoad(MultipartFile file) throws IOException {
        if (file.isEmpty() || file == null) {
            throw new RuntimeException("文件为空");
        }
        if (file.getSize() > 1024L * 1024 * 10) {
            throw new RuntimeException("文件过大，传入图片大小应该在20mb之内");
        }
        String suffix = file.getOriginalFilename().substring
                (file.getOriginalFilename().lastIndexOf(".") + 1
                        , file.getOriginalFilename().length());
        if (!"jpg,jpeg,gif,png,jfif".toUpperCase().contains(suffix.toUpperCase())) {
            throw new RuntimeException("文件类型错误");
        }
        String path = "D:\\nginx-1.22.0\\html\\project_img\\";
//        String path = "/usr/share/nginx/html/cs/";
        String fileName = "";
        fileName += UUID.randomUUID().toString().replace("-", "");
        fileName += System.currentTimeMillis();
        fileName += "." + suffix;
        System.out.println(path + fileName);
        file.transferTo(new File(path + fileName));
        return "http://192.168.60.34/project_img/" + fileName;
//        return "http://120.26.12.207/cs/" + fileName;

    }
}
